package com.web.new12306.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.web.new12306.common.pojo.EasyUIDataGridJSON;
import com.web.new12306.pojo.User;
import com.web.new12306.service.AdminService;
import com.web.new12306.service.UserService;
import com.web.new12306.service.impl.AdminServiceImpl;
import com.web.new12306.service.impl.UserServiceImpl;

/**
 * Servlet implementation class showAll
 */
public class showAll extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		findAllUser(request, response);
		
		
	}
	
	public void findAllUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		System.out.println("11");
		
		AdminService service = new AdminServiceImpl();
		
		int page = 0;
        int rows = 0;
        try {
            page = Integer.valueOf(request.getParameter("page"));
            rows = Integer.valueOf(request.getParameter("rows"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        
        int start = (page - 1) * rows;
        
        List<User> list = service.showAll();
        
        if (list != null) {
			System.out.println("1111111");
		}else {
			System.out.println("33");
		}
       
        
        List<User> pageList = new ArrayList<>();
        for (int i = start; i < start + rows && i < list.size(); i++) {
            pageList.add(list.get(i));
        }
        
        EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
        responseObject.setTotal(list.size());//数据总数，方便显示有多少页，限制页面超出
        responseObject.setRows(pageList);//页面显示数据的list

        response.setContentType("application/json");
        response.getWriter().append(JSON.toJSONString(responseObject));

	}
	

}
